#include "exclude/same_name_task_chain_exclude_policy.h"
#include <logger/logger.h>

namespace exclude
{
class SameNameTaskChainExcludePolicyData
{
private:
    friend class SameNameTaskChainExcludePolicy;
    task::TaskController *task_controller_;
};
}

namespace exclude
{
SameNameTaskChainExcludePolicy::SameNameTaskChainExcludePolicy(task::TaskController *task_controller)
    : d(std::make_unique<SameNameTaskChainExcludePolicyData>())
{
    d->task_controller_ = task_controller;
}
SameNameTaskChainExcludePolicy::~SameNameTaskChainExcludePolicy() = default;

bool SameNameTaskChainExcludePolicy::execPolicy(task::TaskChain *task_chain)
{
    EASE_INFO("execute same task chain name exclude policy");
    auto task_chain_name = task_chain->getName();
    std::vector<std::string> task_chain_name_vec = d->task_controller_->getCurrentTaskChainName();
    for (const auto &chain: task_chain_name_vec)
    {
        if (chain == task_chain_name)
        {
            EASE_ERROR("task chain [%s] is running, cannot start the same named task chain", task_chain_name.c_str());
            return false;
        }
    }
    return true;
}
}
